# -*-coding:utf-8 -*-
"""
专题报表-沪深股票-机构持股-机构持股明细表（按品种）
"""
from EmQuantAPI import *
import common.mainApi as mp
import common.handler_date_time as hd_dt
import common.handler_ctr_result as hd_cr
import common.dataFrame as df
import common.sql as db
import common.mainApi as ma
import config.base_info as bs

base_type = "ctr"
base_target = "target"
base_index = "InstHolderTypeDetailInfo"

info_ = bs.INDEX_INFO.get(base_type).get(base_target).get(base_index)  # ->dict
param_code = info_.get("return_data")  # ->list
param_ = ",".join(param_code)
# "secucode=300059.SZ,reportDate=2019-12-31,OrgType=全部,PredictYear=2019"
option_ = "OrgType=0,reportDate="


def getParamOptionFromSql(tb_name_, page=1, num=100, reportdate="2020-12-31"):
    page_ = (page-1)*num
    sql_ = "SELECT MSECUCODE, REPORTDATE, STR_BAOGAOQI \
    from {0} where REPORTDATE = '{1}' limit {2}, {3}     ".format(
        tb_name_, reportdate, page_, num)
    data_list = db.getdata_from_sql_tolist(sql_)
    return data_list


def mainGetInstHolderTypeDetailInfo(reportdate="2020-12-31", secucode="",  predictYear='2020'):

    data = c.ctr(base_index, param_, option_+reportdate+",secucode=" +
                 secucode+",PredictYear="+predictYear)

    return data


if __name__ == "__main__":
    tb_name = "ctr_instholdertypeinfo"
    
    param_list = getParamOptionFromSql(
        tb_name_=tb_name, page=1, num=100, reportdate="2020-12-31")
    
    mp.login()
    
    for k in param_list:
        if k[0] != "002056.SZ":

            data = mainGetInstHolderTypeDetailInfo(
                reportdate=k[1], secucode=k[0],  predictYear=k[2])
            hd_cr.ctr_result_handler(data_res=data, index_=base_index)
    
    """
    # 603195公牛集团 2020年上市  300607拓斯达 2017年上市
    temp_param_code=[

    ]
    for k in temp_param_code:

        data = mainGetInstHolderTypeDetailInfo(
            reportdate=k[0], secucode=k[1],  predictYear=k[2])
        hd_cr.ctr_result_handler(data_res=data, index_=base_index)
    """

    mp.stop()
    db.dbcon_dispose()  # close-db
